#include "SkinWindowBase.h"
#include "SkinObjManager.h"
#include "SkinUtily.h"
#include "SkinContainerWnd.h"

CSkinWindowBase::CSkinWindowBase()
{
    m_dwStyle = 0;
    m_dwExStyle = 0;
    SetRect( &m_rcWindow, 0, 0, 0, 0 );
    m_bShow = TRUE;
    m_clrBk = CLR_INVALID;
    m_realHwnd = NULL;
    m_realParentHwnd = NULL;
    m_pContainer = NULL;
    m_wndSizeType = SIZE_WIDTH_FIXPARENT | SIZE_HEIGHT_FIXPARENT;
    m_left = 0;
    m_top = 0;
}

CSkinWindowBase::~CSkinWindowBase()
{
}
//
// BOOL CSkinWindowBase::CreateSkinObject()
// {
//     return TRUE;
// }
//
// BOOL CSkinWindowBase::ShowSkinObject(BOOL bShow)
// {
//     m_bShow=bShow;
//     return TRUE;
// }
//
// HSKINHWND CSkinWindowBase::GetSkinHwnd()
// {
//     return m_skinHwnd;
// }
//
// HSKINHWND CSkinWindowBase::GetParentSkinHwnd()
// {
//     return m_skinHwndParent;
// }
//
// void CSkinWindowBase::SetParentSkinHwnd(HSKINHWND hParent)
// {
//     m_skinHwndParent=hParent;
// }
//
// HWND CSkinWindowBase::GetHwndReal()
// {
//     return m_hWndReal;
// }
//
// void CSkinWindowBase::SetHwndReal(HWND hWnd)
// {
//     m_hWndReal=hWnd;
// }
//
// BOOL CSkinWindowBase::IsShow()
// {
//     return m_bShow;
// }

DWORD CSkinWindowBase::GetSkinStyle()
{
    return m_dwStyle;
}

DWORD CSkinWindowBase::GetSkinExStyle()
{
    return m_dwExStyle;
}

CRect CSkinWindowBase::GetSkinRect()
{
    return m_rcWindow;
}

void CSkinWindowBase::GetSkinRect( CRect& rc )
{
    rc = m_rcWindow;
}

LPCTSTR CSkinWindowBase::GetSkinCaption()
{
    return m_szWindowName;
}

COLORREF CSkinWindowBase::GetBkClr()
{
    return m_clrBk;
}

BOOL CSkinWindowBase::CreateSkinObject( CSkinWindowBase* pParent )
{
    //     if( NULL == pParent )
    //     {
    //         m_hWndParent = NULL;
    //     }
    //     else
    //     {
    //         m_hWndParent = pParent->GetHwnd();
    //     }
    return TRUE;
}

void CSkinWindowBase::OnPaint( HDC hDC )
{
    CDCHandle dc( hDC );
    dc.FillSolidRect( &m_rcWindow, m_clrBk );
}

BOOL CSkinWindowBase::LoadNode( IXMLDOMNode* pNode )
{
    BOOL bRet = __super::LoadNode( pNode );

    if ( bRet )
    {
        m_rcWindow.left = m_left;
        m_rcWindow.top = m_top;
        int widthPercent = 0;

        if ( CSkinUtily::String2Percent( m_width, &widthPercent ) )
        {
            m_wndSizeType = ( m_wndSizeType& ~SIZE_WIDTH_MASK ) | SIZE_WIDTH_FIXPARENT;
        }
        else
        {
            if ( !m_width.IsEmpty() )
            {
                m_wndSizeType = ( m_wndSizeType& ~SIZE_WIDTH_MASK ) | SIZE_WIDTH_SPECIAL;
                m_rcWindow.right = _ttol( m_width );
            }
        }

        int heightPercent = 0;

        if ( CSkinUtily::String2Percent( m_height, &heightPercent ) )
        {
            m_wndSizeType = ( m_wndSizeType& ~SIZE_HEIGHT_MASK ) | SIZE_HEIGHT_FIXPARENT;
        }
        else
        {
            if ( !m_height.IsEmpty() )
            {
                m_wndSizeType = ( m_wndSizeType& ~SIZE_HEIGHT_MASK ) | SIZE_HEIGHT_SPECIAL;
                m_rcWindow.bottom = _ttol( m_height );
            }
        }
    }

    return bRet;
}

CSkinWindowBase* CSkinWindowBase::CreateObject( LPCTSTR lpszClassName )
{
    CSkinObjManager* pObjMgr = CSkinObjManager::GetHinstance();
    CSkinWindowBase* pWndBase = pObjMgr->CreateObject( lpszClassName );
    m_skinHwnd = CSkinObjManager::GetHinstance()->AddObject( pWndBase );
    return pWndBase;
}

BOOL CSkinWindowBase::IsMsgHandled() const
{
    return m_bMsgHandled;
}

void CSkinWindowBase::SetMsgHandled( BOOL bHandled )
{
    m_bMsgHandled = bHandled;
}

LRESULT CSkinWindowBase::SendSkinMessage( UINT Msg, WPARAM wParam /*= 0*/, LPARAM lParam /*= 0*/ )
{
    SetMsgHandled( FALSE );
    return 0;
}

void CSkinWindowBase::OnWindowPosChanged( LPWINDOWPOS lpWndPos )
{
    m_rcWindow.MoveToXY( lpWndPos->x, lpWndPos->y );
    SIZE szRect = {lpWndPos->cx, lpWndPos->cy};
    OnNcCalcSize( TRUE, ( LPARAM ) &szRect );
    m_rcWindow.SetRect( lpWndPos->x, lpWndPos->y, lpWndPos->x + szRect.cx, lpWndPos->y + szRect.cy );
    m_rcWindow.OffsetRect( m_left, m_top );
// 	if( SIZE_WIDTH_FIXPARENT == ( GetSizeType() & SIZE_WIDTH_FIXPARENT ) )
// 	{
// 		m_rcWindow.right = lpWndPos->x+lpWndPos->cx;
// 	}
// 	else if( SIZE_WIDTH_SPECIAL == ( GetSizeType() & SIZE_WIDTH_SPECIAL ) )
// 	{
// 		m_rcWindow.right = lpWndPos->x+_ttol( m_width );
// 	}

//     lpWndPos->x = m_left;
//     lpWndPos->y = m_rcWindow.Height();
}

LRESULT CSkinWindowBase::OnNcCalcSize( BOOL bCalcValidRects, LPARAM lParam )
{
    LPSIZE pSize = ( LPSIZE )lParam;
    //	CRect rect;
    //	rect.SetRect(m_pContainer->m_rcWindow.TopLeft(),m_pContainer->m_rcWindow.BottomRight());
    CRect rcWindow;
    rcWindow.SetRectEmpty();

    if ( SIZE_WIDTH_FIXPARENT == ( GetSizeType() & SIZE_WIDTH_FIXPARENT ) )
    {
        rcWindow.right = m_pContainer->m_rcWindow.Width();
    }
    else if ( SIZE_WIDTH_SPECIAL == ( GetSizeType() & SIZE_WIDTH_SPECIAL ) )
    {
        rcWindow.right = _ttol( m_width );
    }

    if ( SIZE_HEIGHT_FIXPARENT == ( GetSizeType() & SIZE_HEIGHT_FIXPARENT ) )
    {
        rcWindow.bottom = m_pContainer->m_rcWindow.Height();
    }
    else if ( SIZE_HEIGHT_SPECIAL == ( GetSizeType() & SIZE_HEIGHT_SPECIAL ) )
    {
        rcWindow.bottom = _ttol( m_height );
    }

// 	if( TRUE )
// 	{
// 		// 		CRect rcParent;
// 		// 		::GetClientRect( GetRealParentHwnd(), &rcParent );
// 		//		m_rcWindow.left=pSize->cx;
// 		//		m_rcWindow.top=pSize->cy;
// 		int widthPercent = 0;
//
// 		if( CSkinUtily::String2Percent( m_width, &widthPercent ) )
// 		{
// 			rcWindow.right = m_pContainer->m_rcWindow.Width() * widthPercent / 100;
// 		}
// 		else
// 			rcWindow.right = _ttoi( m_width );
//
// 		int heightPercent = 0;
//
// 		if( CSkinUtily::String2Percent( m_height, &heightPercent ) )
// 		{
// 			rcWindow.bottom = m_pContainer->m_rcWindow.Height() * heightPercent / 100;
// 		}
// 		else
// 			rcWindow.bottom = _ttoi( m_height );
// 	}
    *pSize = rcWindow.Size();
    return 0;
}

void CSkinWindowBase::SetRealParentHwnd( HWND hParent )
{
    m_realParentHwnd = hParent;
}

HSKINHWND CSkinWindowBase::GetSkinHwnd()
{
    return m_skinHwnd;
}

HWND CSkinWindowBase::GetRealParentHwnd()
{
    if ( NULL == m_realParentHwnd )
    {
        m_realParentHwnd =::GetActiveWindow();

        if ( NULL == m_realParentHwnd )
        {
            m_realParentHwnd =::GetDesktopWindow();
        }
    }

    return m_realParentHwnd;
}

void CSkinWindowBase::SetContainer( CSkinContainerWnd* pContainer )
{
    ATLASSERT( NULL == m_pContainer );
    m_pContainer = pContainer;
}

HWND CSkinWindowBase::GetRealHwnd() const
{
    return m_realHwnd;
}

void CSkinWindowBase::SetRealHwnd( HWND realHwnd )
{
    m_realHwnd = realHwnd;
}

CSkinContainerWnd* CSkinWindowBase::GetContainer() const
{
    return m_pContainer;
}

void CSkinWindowBase::NodeLoadComplete()
{
}

UINT CSkinWindowBase::GetSizeType()
{
    return m_wndSizeType;
}


